// miniprogram/pages/mine/activity/add/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    point:0,
    beginDate: '',
    endDate: '',
    isBeginDate: false,
    isEndDate: false,
    show: false,
    minHour: 0,
    maxHour: 23,
    minDate: new Date().getTime(),
    maxDate: new Date(2030, 10, 1).getTime(),
    currentDate: new Date().getTime(),
  },

  onClickAdd: function () {
    const {beginDate, name,endDate,point} = this.data;
    if (!beginDate||!name||!endDate||!point) {
      wx.showModal({
        title: "提交错误",
        content: "请输入必填项",
        confirmText: "我知道了",
        showCancel: false,
        success: function (res) {},
      });
      return;
    }
    wx.showLoading({
      title: '加载中...',
    });
    
    wx.cloud.callFunction({
      name: 'addActivity',
      data: {
        name: this.data.name,
        point: this.data.point,
        beginTime: this.data.beginDate,
        endTime:this.data.endDate
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        if (res.result.success) {
          wx.showModal({
            content: res.result.message,
            confirmText: '我知道了',
            showCancel: false,
            success: function (res) {
              wx.navigateBack()
            }
          })
        } else {
          wx.showModal({
            content: res.result.message,
            confirmText: '我知道了',
            showCancel: false,
            success: function (res) {
            }
          })
        }

      },
      fail: err => {
        wx.hideLoading()
      }
    })
  },
  onClickBeginDate() {
    this.setData({
      isEndDate: false,
      isBeginDate: true,
      show: true
    });
  },
  onClickEndDate() {
    this.setData({
      isBeginDate: false,
      isEndDate: true,
      show: true
    });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    console.log(date)
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
  },
  onConfirm(event) {
    if (this.data.isBeginDate) {
      this.setData({
        show: false,
        beginDate: this.formatDate(event.detail),
      })
    } else {
      this.setData({
        show: false,
        endDate: this.formatDate(event.detail),
      })
    }
  },
  onCancel() {
    this.setData({
      show: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})